<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="赛事名称" prop="name">
                    <el-input class="w-[280px]" v-model="queryParams.name" placeholder="请输入赛事名称"/>
                </el-form-item>
                <el-form-item label="赛事地点" prop="address">
                    <el-input class="w-[280px]" v-model="queryParams.address" placeholder="请输入赛事地点"/>
                </el-form-item>
                <el-form-item label="赛事时间" prop="playTime">
                    <el-input class="w-[280px]" v-model="queryParams.playTime" placeholder="请输入赛事时间"/>
                </el-form-item>
                <el-form-item label="比赛方式" prop="playType">
                    <el-select v-model="queryParams.playType" class="w-[280px]" placeholder="请选择比赛方式" clearable>
                      <el-option label="男单打" :value="1" />
                      <el-option label="女单打" :value="2" />
                      <el-option label="男双打" :value="3" />
                      <el-option label="女双打" :value="4" />
                      <el-option label="男女混打" :value="5" />
                    </el-select>
                </el-form-item>
                <el-form-item label="赛事赛程" prop="schedule">
                  <el-select v-model="queryParams.schedule" class="w-[280px]" placeholder="请选择赛事赛程" clearable>
                    <el-option label="初赛" :value="1" />
                    <el-option label="半决赛" :value="2" />
                    <el-option label="总决赛" :value="3" />
                  </el-select>
                </el-form-item>
<!--                <el-form-item label="组织方&nbsp;&nbsp;&nbsp;" prop="orgParty">-->
<!--                    <el-input class="w-[280px]" v-model="queryParams.orgParty" placeholder="请输入组织方"/>-->
<!--                </el-form-item>-->
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
              <router-link
                  v-perms="['competition.list/add', 'article.article/add:edit']"
                  :to="{
                        path: getRoutePath('competition.list/add:edit')
                    }"
              >
                <el-button type="primary" class="mb-4">
                  <template #icon>
                    <icon name="el-icon-Plus" />
                  </template>
                  新增
                </el-button>
              </router-link>
            </div>
            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="名称" prop="name" min-width="100">
                  <template #default="{ row }">
                    <el-button
                        type="primary"
                        link
                    >
                      <router-link
                          :to="{
                                        path: getRoutePath('competition.list/detail'),
                                        query: {
                                            id: row.id
                                        }
                                    }"
                      >
                        {{row.name}}
                      </router-link>
                    </el-button>
                  </template>

                </el-table-column>
                <el-table-column label="封面" min-width="60">
                    <template #default="{ row }">
                        <image-contain
                            v-if="row.cover"
                            :src="row.cover"
                            :width="60"
                            :height="45"
                            :preview-src-list="[row.cover]"
                            preview-teleported
                            fit="contain"
                        />
                    </template>
                </el-table-column>
<!--                <el-table-column label="地点" prop="address" min-width="100" />-->
                <el-table-column label="比赛时间" prop="playTime" min-width="130">
                  <template #default="{row}">
                    {{row.playTime}}~{{row.playEndTime}}
                  </template>
                </el-table-column>
                <el-table-column label="比赛方式" min-width="70">
                    <template #default="{ row }">
                        <el-tag v-if="row.playType.includes('0')||(row.playType.includes('1')&&row.playType.includes('2')&&row.playType.includes('3')&&row.playType.includes('4')&&row.playType.includes('5'))">全类型</el-tag>
                        <el-tag v-if="row.playType.includes('1')">男单打</el-tag>&nbsp;
                        <el-tag type="warning" v-if="row.playType.includes('2')">女单打</el-tag>&nbsp;
                        <el-tag type="success" v-if="row.playType.includes('3')">男双打</el-tag>&nbsp;
                        <el-tag type="info" v-if="row.playType.includes('4')">女双打</el-tag>&nbsp;
                        <el-tag type="danger" v-if="row.playType.includes('5')">男女混打</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="赛程" prop="schedule" min-width="60">
                  <template #default="{ row }">
                    <el-tag v-if="row.schedule == 1">初赛</el-tag>
                    <el-tag type="danger" v-else-if="row.schedule == 2">半决赛</el-tag>
                    <el-tag type="danger" v-else-if="row.schedule == 3">总决赛</el-tag>
                  </template>
                </el-table-column>
                <el-table-column label="状态" prop="status" min-width="60">
                  <template #default="{ row }">
                    <el-tag v-if="row.status == 0">待审核</el-tag>
                    <el-tag type="success" v-else-if="row.status == 1">正常</el-tag>
                    <el-tag type="info" v-else-if="row.status == 5">结束</el-tag>
                    <el-tag type="danger" v-else-if="row.status == 2">不通过</el-tag>
                  </template>
                </el-table-column>
<!--                <el-table-column label="组织方" prop="orgParty" min-width="120" />-->
<!--                <el-table-column label="创建者" prop="createId" min-width="120" />-->
                <el-table-column label="创建时间" prop="createTime" min-width="140" />
                <el-table-column label="操作" width="160" fixed="right">
                    <template #default="{ row }">
                      <el-button
                          v-perms="['competition.list/examine']"
                          v-if="row.status == 0"
                          type="primary"
                          link
                          @click="handleExamine(row)"
                      >
                        审核
                      </el-button>
                      <el-button
                          v-perms="['article.article/edit', 'article.article/add:edit']"
                          type="primary"
                          link
                      >
                        <router-link
                            :to="{
                                    path: getRoutePath('competition.list/add:edit'),
                                    query: {
                                        id: row.id
                                    }
                                }"
                        >
                          编辑
                        </router-link>
                      </el-button>
                      <el-button
                          v-perms="['competition.list/del']"
                          type="danger"
                          link
                          @click="handleDelete(row.id)"
                      >
                          删除
                      </el-button>
                      <el-button
                          v-if="row.status != 0"
                          type="primary"
                          link
                      >
                        <router-link
                            :to="{
                                    path: getRoutePath('competition.list/detail'),
                                    query: {
                                        id: row.id
                                    }
                                }"
                        >
                          详情
                        </router-link>
                      </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="competition">
import { competitionDelete, competitionLists,applyExamine } from '@/api/competition/competition'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import { getRoutePath } from '@/router'
import EditPopup from './edit.vue'
import type {Action} from "element-plus";
import {ElMessageBox} from "element-plus";
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    name: '',
    cover: '',
    address: '',
    playTime: '',
    playType: '',
    schedule: '',
    status: '',
    orgParty: '',
    orgId: '',
    createId: '',
    updateId: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: competitionLists,
    params: queryParams
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await competitionDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

const handleExamine = async (obj: any) => {
  let status = obj.status;
  await ElMessageBox.confirm('审核提交赛事【'+obj.name+'】是否通过？','温馨提示', {
    distinguishCancelAndClose: true,
    confirmButtonText: '确定',
    cancelButtonText: '拒绝',
    type: 'warning'
  }).then(() => {
    status = 1
  }).catch((action: Action) => {
    console.log(action)
    if(action === 'cancel'){
      status = 2
    }else{
      status = 0
    }
  })
  if(status !=0){
    await applyExamine({ id:obj.id,status:status })
  }
  getLists()
}

getLists()
</script>
